package com.readbook.wxutil;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.readbook.bean.Template;


/**
 * @author zs
 * 接口工具类
 */
public class AdvancedUtil {
    private static Logger log = LoggerFactory.getLogger(AdvancedUtil.class);

    /**
     * 发送模板消息
     *
     * @param accessToken
     * @return
     * @template template
     */
    public static String sendTemplateMessage(String accessToken, Template template) {
        if ("".equals(accessToken) || template == null) {
            return "{success:false,msg:'获取数据失败！'}";
        }
        // 拼接请求地址
        String requestUrl = WxMessageUtil.xcx;
        requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken).replace("NEXT_OPENID", accessToken);
        //发送模板消息
        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "POST", template.toJSON());
        //如果请求成功
        int errorCode = jsonObject.getInt("errcode");
        String errorMsg = jsonObject.getString("errmsg");
        if (0 == errorCode && "ok".equals(errorMsg)) {
            return "{success:true,msg:'发送成功！'}";
        } else {
            return "{success:false,msg:'对不起，该用户没有关注该公众号或者已经取消了关注！'}";
        }
    }
}
